using Engine;
using Game;
using GameEntitySystem;

namespace SCIENEW {

	public class HearthFurnaceCastingBasinDeviceBehavior : EntityDeviceBehavior<ComponentHearthFurnaceCastingBasin> {
        public SubsystemPickables m_subsystemPickable;
        public SubsystemAudio m_subsystemAudio;

        public HearthFurnaceCastingBasinDeviceBehavior(Project project) : base(project) {
            m_subsystemPickable = project.FindSubsystem<SubsystemPickables>(true);
            m_subsystemAudio = project.FindSubsystem<SubsystemAudio>(true);
        }

        public override bool OnInteract(TerrainRaycastResult raycastResult, ComponentMiner componentMiner) {
            ComponentBlockEntity componentBlockEntity = m_subsystemBlockEntities.GetBlockEntity(raycastResult.CellFace.X, raycastResult.CellFace.Y, raycastResult.CellFace.Z);
            if (componentBlockEntity == null) {
                componentMiner.ComponentPlayer.ComponentGui.DisplaySmallMessage("浇筑盆貌似坏了，重新放置一下吧~", Color.White, false, false);
                return false;
            }
            ComponentHearthFurnaceCastingBasin componentCastingBasin = componentBlockEntity.Entity.FindComponent<ComponentHearthFurnaceCastingBasin>();
            if (componentCastingBasin == null) {
                componentMiner.ComponentPlayer.ComponentGui.DisplaySmallMessage("浇筑盆貌似坏了，重新放置一下吧~", Color.White, false, false);
                return false;
            }

            Vector3 basinToPlayer = componentMiner.ComponentPlayer.ComponentBody.Position - (new Vector3(raycastResult.CellFace.Point) + new Vector3(0.5f));
            basinToPlayer = new Vector3(basinToPlayer.X, 0, basinToPlayer.Z);
            basinToPlayer = Vector3.Normalize(basinToPlayer);

            //存在内容物，优先弹出内容物
            if (componentCastingBasin.GetSlotCount(componentCastingBasin.ResultSlotIndex) > 0) {
                m_subsystemPickable.AddPickable(componentCastingBasin.GetSlotValue(componentCastingBasin.ResultSlotIndex), 1, new Vector3(raycastResult.CellFace.Point) + new Vector3(0.5f, 0.75f, 0.5f), Vector3.UnitY * 2 + basinToPlayer * 3, null);
                componentCastingBasin.RemoveSlotItems(componentCastingBasin.ResultSlotIndex, 1);
                m_subsystemAudio.PlaySound("Audio/BlockPlaced", 1f, 0.3f, new Vector3(raycastResult.CellFace.Point), 2.5f, autoDelay: true);
                return true;
            }

            //不存在内容物，弹出模具
            int blockValue = componentMiner.ActiveBlockValue;
            int blockIndex = Terrain.ExtractContents(blockValue);
            if (componentCastingBasin.GetSlotCount(componentCastingBasin.ModuleSlotIndex) > 0) {//存在旧模具，弹出旧模具
                m_subsystemPickable.AddPickable(componentCastingBasin.GetSlotValue(componentCastingBasin.ModuleSlotIndex), 1, new Vector3(raycastResult.CellFace.Point) + new Vector3(0.5f, 0.75f, 0.5f), Vector3.UnitY * 2 + basinToPlayer * 3, null);
                componentCastingBasin.RemoveSlotItems(componentCastingBasin.ModuleSlotIndex, 1);
            }
            if (blockIndex == BaseItemBlock.Index
                && ItemsBlockManager.GetItem(blockValue) is ModuleItem) {//手持新模具，加入新模具
                IInventory inventory = componentMiner.Inventory;
                int activeSlotIndex = componentMiner.Inventory.ActiveSlotIndex;
                if (ComponentInventoryBase.AcquireItems(componentCastingBasin, blockValue, 1) == 0) {//接受物品
                    inventory.RemoveSlotItems(activeSlotIndex, 1);//移除手上的物品
                    m_subsystemAudio.PlaySound("Audio/BlockPlaced", 1f, 0.3f, new Vector3(raycastResult.CellFace.Point), 2.5f, autoDelay: true);
                    return true;
                }
            }
            return true;
        }
    }
}